package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func indexHandler1(c *gin.Context) {
	fmt.Println("index in ...")
	c.JSON(http.StatusOK, gin.H{
		"msg": "index",
	})
}

// 定义一个中间件:统计耗时
func m11(c *gin.Context) {
	fmt.Println("m11 in...")
	// 计时
	start := time.Now()
	// c.Abort() //阻止调用后续的处理函数
	c.Next() // 调用后续的处理函数 执行indexHandler函数
	cost := time.Since(start)
	fmt.Printf("cost:%v\n", cost)
	fmt.Println(cost)
	//输出
	// m1 in ....
	//index in ...
	//cost:%v
	// 996.8µs
}

func main() {
	r := gin.Default()
	//GET(relativePath string, handlers ...HandlerFunc) IRoutes
	r.GET("/index", m11, indexHandler1) //先执行m1函数再执行indexHandler函数

	r.Run(":9090")
}
